<template>
	<a-card :bordered="false">
		<a-form border="false" ref="searchFormRef" :model="searchFormState">
			<a-row :gutter="24">
				<a-col :span="4">
					<a-button type="primary" @click="tableRef.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false" class="mt-2">
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:scroll="{ x: 'max-content' }"
		>
			<template #operator>
				<a-space>
					<a-button type="primary" @click="formRef.onOpen"> 新增</a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'content'">
					<span class="ellipsis-text">{{ record.content }}</span>
				</template>
				<template v-else-if="column.dataIndex === 'action'">
					<a-space>
						<a-button type="link" primary size="small" @click="formRef.onOpen(record)">编辑</a-button>
						<a-divider type="vertical" />
						<a-popconfirm title="确定要删除吗？" @confirm="del(record.id)">
							<a-button type="link" danger size="small">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()" />
</template>

<script setup name="accessLog">
	import articlesApi from '@/api/website/articles/articlesApi'
	import Form from '@/views/website/articles/form.vue'

	const searchFormRef = ref()
	const searchFormState = ref({})
	const tableRef = ref()
	const formRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: false, striped: false }
	const columns = [
		{
			title: '调解中心',
			dataIndex: 'companyName',
			align: 'center'
		},
		{
			title: '类型',
			dataIndex: 'typeName',
			align: 'center'
		},
		{
			title: '标题',
			dataIndex: 'title',
			align: 'center'
		},
		{
			title: '文章',
			dataIndex: 'content',
			align: 'center',
			width: 200, // 设置固定宽度
			ellipsis: true // 启用省略号
		},
		{
			title: '创建人',
			dataIndex: 'createUserName',
			align: 'center'
		},
		{
			title: '创建时间',
			dataIndex: 'createTime',
			align: 'center'
		},
		{
			title: '修改人',
			dataIndex: 'updateUserName',
			align: 'center'
		},
		{
			title: '修改时间',
			dataIndex: 'updateTime',
			align: 'center'
		},
		{
			title: '操作',
			dataIndex: 'action',
			align: 'center'
		}
	]
	const loadData = (parameter) => {
		searchFormState.value.current = parameter.current
		searchFormState.value.size = parameter.size
		return articlesApi.pageList(searchFormState.value).then((res) => {
			return res
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}

	const del = (id) => {
		articlesApi.del(id).then((res) => {
			tableRef.value.refresh()
		})
	}
</script>

<style scoped>
	.ellipsis-text {
		display: inline-block;
		width: 100%;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
		max-width: 200px; /* 与列宽保持一致 */
	}
</style>
